package com.seguriapp.common.gwt.client.searchfilter;

import java.util.Date;
import java.util.List;

import com.google.gwt.event.shared.EventBus;
import com.seguriapp.common.gwt.client.SeguriAppDisplay;
import com.seguriapp.common.gwt.client.SeguriAppPresenter;
import com.seguriapp.common.gwt.client.UtilClientValidations;
import com.seguriapp.entities.SeguriAppEntity;

public class SearchFilterPresenter implements SeguriAppPresenter {

  private final Display display;
  private EventBus eventBus;
  private List<SeguriAppEntity> policeStationList;

  public SearchFilterPresenter(final EventBus eventBus, final Display display, final List<SeguriAppEntity> policeStationList) {
    this.eventBus = eventBus;
    display.setPresenter(this);
    this.display = display;
    this.policeStationList = policeStationList;
  }

  public void start() {
    display.setPoliceStationList(policeStationList);
  }

  public void applyFilters(final String startDateStr, final String endDateStr, final String names, final double latitude,
      final double longitude, final String distance, final String policeStationId, final boolean searchByPolStation) {
    final Date startDate = startDateStr == null || startDateStr.isEmpty()? null : UtilClientValidations.DATE_FORMAT.parse(startDateStr);
    final Date endDate = endDateStr == null || endDateStr.isEmpty() ? null : UtilClientValidations.DATE_FORMAT.parse(endDateStr);
    final int dictanceInt = Integer.valueOf(distance);
    final long pStationId = Long.valueOf(policeStationId);
    eventBus.fireEvent(new SearchFilterAppliedEvent(startDate, endDate, names, latitude, longitude, dictanceInt, pStationId, searchByPolStation));
  }

  public interface Display extends SeguriAppDisplay {

    /**
     * setea el presenter del activity
     */
    void setPresenter(SearchFilterPresenter presenter);

    void setPoliceStationList(List<SeguriAppEntity> policeStationList);

  }

}
